import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";

var _dec, _dec2, _class;

import React, { cloneElement } from 'react';
import { Bind } from 'lodash-decorators';
import { getResponse } from "../../../utils/utils";
import { checkPermission } from "../../../services/api";
/**
 * 处理表格列，检查权限
 */

var TablePermission = (_dec = Bind(), _dec2 = Bind(), (_class = /*#__PURE__*/function (_React$Component) {
  _inherits(TablePermission, _React$Component);

  var _super = _createSuper(TablePermission);

  function TablePermission() {
    var _this;

    _classCallCheck(this, TablePermission);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      checkColumns: []
    };
    _this.codeQueue = new Set();
    return _this;
  }

  _createClass(TablePermission, [{
    key: "componentDidMount",
    value: // eslint-disable-next-line
    function componentDidMount() {
      this.check();
    }
  }, {
    key: "componentWillUnmount",
    value: function componentWillUnmount() {
      this.codeQueue.clear();
    }
  }, {
    key: "check",
    value: function check() {
      var _this2 = this;

      var children = this.props.children;
      var _children$props$colum = children.props.columns,
          columns = _children$props$colum === void 0 ? [] : _children$props$colum;
      var checkColumns = []; // 收集列中的权限集，存放在队列中

      columns.forEach(function (item) {
        var permissionList = item.permissionList;

        if (permissionList && Array.isArray(permissionList)) {
          permissionList.forEach(function (key) {
            _this2.codeQueue.add(key.code.replace(/^\//g, '').replace(/\//g, '.').replace(/:/g, '-'));
          });
        }
      });

      if (this.codeQueue.size > 0) {
        checkPermission(Array.from(this.codeQueue)).then(function (data) {
          if (getResponse(data)) {
            // 将列中的权限集与鉴权结果做比对，去除鉴权失败的列
            columns.forEach(function (items) {
              data.some(function (_ref) {
                var code = _ref.code,
                    approve = _ref.approve;

                if (items.permissionList === undefined || // approved=true，则controllerType=disabled则禁用，如果为hidden，则隐藏。非这两个值，则不控制
                items.permissionList.some(function (key) {
                  return key.code.replace(/^\//g, '').replace(/\//g, '.') === code;
                }) && approve) {
                  checkColumns.push(items);
                }

                return items.permissionList === undefined || items.permissionList.some(function (key) {
                  return key.code === code;
                }) && approve;
              });
            });
          }

          _this2.setState({
            checkColumns: checkColumns
          });
        });
      } else {
        this.setState({
          checkColumns: columns
        });
      }
    }
  }, {
    key: "extendProps",
    value: function extendProps() {
      var children = this.props.children;
      var checkColumns = this.state.checkColumns;
      return cloneElement(children, {
        columns: checkColumns
      });
    }
  }, {
    key: "render",
    value: function render() {
      return this.extendProps();
    }
  }]);

  return TablePermission;
}(React.Component), (_applyDecoratedDescriptor(_class.prototype, "check", [_dec], Object.getOwnPropertyDescriptor(_class.prototype, "check"), _class.prototype), _applyDecoratedDescriptor(_class.prototype, "extendProps", [_dec2], Object.getOwnPropertyDescriptor(_class.prototype, "extendProps"), _class.prototype)), _class));
export { TablePermission as default };